package com.itop.automator.weixin;

import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiSelector;

import com.itop.automator.commom.Constants;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

@RunWith(AndroidJUnit4.class)
public class WeiXinTest {

    private Context mContext;
    private UiDevice mUIDevice;

    @Test
    public void useAppContext() throws Exception {
        // Context of the app under test.
        mContext = InstrumentationRegistry.getTargetContext();

        assertEquals("com.example.test.wingtech", mContext.getPackageName());
    }

    @Before
    public void setUp() throws RemoteException {
        mUIDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());  //获得device对象

        if (!mUIDevice.isScreenOn()) {  //唤醒屏幕
            mUIDevice.wakeUp();
        }
        mUIDevice.pressHome();  //按home键
    }

    /**
     * 启动app
     */
    @Test
    public void startWeixin() {
        mContext = InstrumentationRegistry.getTargetContext();
        Intent myIntent = mContext.getPackageManager().getLaunchIntentForPackage(Constants.PACKAGE_NAME_WEIXIN);
        mContext.startActivity(myIntent);
    }

    @Test
    public void sendMsg() {
        mContext = InstrumentationRegistry.getTargetContext();
        Intent myIntent = mContext.getPackageManager().getLaunchIntentForPackage(Constants.PACKAGE_NAME_WEIXIN);  //启动app
        mContext.startActivity(myIntent);
        mUIDevice.waitForWindowUpdate(Constants.PACKAGE_NAME_WEIXIN, 5 * 2000);

        //Pattern mPattern = new Pattern();

        UiObject inputEdit = mUIDevice.findObject(new UiSelector().resourceId(Constants.PACKAGE_NAME_WEIXIN + ":id/edit_content"));
        //UiObject2 inputEdit = mUIDevice.findObject(By.res(PACKAGE_NAME, "edit_content"));
        try {
            inputEdit.setText("test succ");
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        UiObject sendBtn = mUIDevice.findObject(new UiSelector().text("SEND"));  //定位text内容为Send的控键
        try {
            sendBtn.click();  //点击按键
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //assertTrue(true); //断言，随便乱写的，此处未起到断言作用
    }

}
